package com.alit.face.koala.util;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * org.json工具类
 * @author litong
 *
 */
public class JsonUtil {
  public static int getInt(org.json.JSONObject jsonObject, String key) {
    int errorCode = -1;
    try {
      errorCode = jsonObject.getInt(key);
      return errorCode;
    } catch (JSONException e) {
      e.printStackTrace();
    }
    return 0;
  }

  public static String getString(org.json.JSONObject jsonObject, String key) {
    String string = null;
    try {
      string = jsonObject.getString(key);
    } catch (JSONException e) {
      e.printStackTrace();
    }
    return string;
  }

  public static org.json.JSONObject getJSONObject(org.json.JSONObject jsonObject, String key) {
    org.json.JSONObject result = null;
    try {
      result = jsonObject.getJSONObject(key);
    } catch (JSONException e) {
      e.printStackTrace();
    }
    return result;
  }

  public static org.json.JSONArray getJSONArray(org.json.JSONObject jsonObject, String key) {
    org.json.JSONArray jsonArray = null;
    try {
      jsonArray = jsonObject.getJSONArray(key);
    } catch (JSONException e1) {
      e1.printStackTrace();
    }
    return jsonArray;
  }

  public static org.json.JSONObject getJSONObject(org.json.JSONArray jsonArray, int index) {
    org.json.JSONObject jsonObject = null;
    try {
      jsonObject = jsonArray.getJSONObject(index);
    } catch (JSONException e1) {
      e1.printStackTrace();
    }
    return jsonObject;
  }

  public static org.json.JSONObject parseJsonObject(String string) {
    org.json.JSONObject jsonObject = null;
    try {
      jsonObject = new org.json.JSONObject(string);
    } catch (JSONException e) {
      e.printStackTrace();
    }
    return jsonObject;
  }
  
  public static double getDouble(JSONObject jsonObject,String key){
    double score=0;
    try {
      score = jsonObject.getDouble( key);
    } catch (JSONException e) {
      e.printStackTrace();
    }
    return score;
  }
}
